package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.model.PageBbPemasokBean;
import id.net.iconpln.bb.client.service.MasterRPCService;
import id.net.iconpln.bb.client.service.MasterRPCServiceAsync;
import id.net.iconpln.bb.client.service.TransportRPCService;
import id.net.iconpln.bb.client.service.TransportRPCServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.store.StoreListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbPengajuanPermintaanHarian extends LayoutContainer {
	private final TransportRPCServiceAsync service = GWT.create(TransportRPCService.class);
	private final MasterRPCServiceAsync serviceMaster = GWT.create(MasterRPCService.class);
	private Grid<PageBbPemasokBean> grid;
	private ListStore storeResult, storeUnit;
	private ComboBox cboUnit;
	private TextField txtPemakaian, txtNoPermintaan;
	private TextArea txtKeterangan;
	private DateField txtTgl;
	
	@Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout()); 
	    
	    ContentPanel cp = new ContentPanel();  
	  
	    cp.setHeading("Daftar Pengajuan Pemakaian");  
	    cp.setFrame(true);  
	    cp.setSize(1030, 600);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));  
	  
	    FormPanel panel = createForm();
	    
	    cp.add(panel, new RowData(.4, 1));  
	    add(cp, new FitData(5)); 
	  }  
	  
	  private FormPanel createForm() {  
		  storeUnit = new ListStore();
		  storeResult = new ListStore();
		  storeResult.addStoreListener(new StoreListener<ModelData>(){
			  @Override
				public void storeAdd(StoreEvent<ModelData> se) {
					// TODO Auto-generated method stub
					super.storeAdd(se);
					MessageBox.alert("Information", se.getModels().get(0).get("message").toString(), null);  
				}			  
		  });
		  
		  ContentPanel p = new ContentPanel();
		  p.setHeaderVisible(false);	    
		  p.setButtonAlign(HorizontalAlignment.RIGHT);  
		  final FormPanel panel = new FormPanel();  
		  panel.setHeaderVisible(false);
	    
		  FieldSet fieldSet2 = new FieldSet();  
		  fieldSet2.setHeading("Pengajuan Pakai");  
		  fieldSet2.setCollapsible(false);
	
		  FormLayout layout2 = new FormLayout();  
		  layout2.setLabelWidth(150);  
		  fieldSet2.setLayout(layout2); 
	    
		  txtNoPermintaan = new TextField();
		  txtNoPermintaan.setName("no_permintaan");
		  txtNoPermintaan.setFieldLabel("No. Permintaan");  
		  txtNoPermintaan.setReadOnly(true);
		  fieldSet2.add(txtNoPermintaan);
		  
		  
		  Map map = new FastMap();
		  map.put("id_pltu", Registry.get("idPltu"));
		  serviceMaster.getMaster("UNIT", map, new AsyncCallback<List<Map>>() {
			
			@Override
			public void onSuccess(List<Map> result) {
				// TODO Auto-generated method stub
				GWT.log("result : " + result.size());
				storeUnit.removeAll();
				storeUnit.add(ClientModule.setListBaseModelFromListMap(result));
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				GWT.log(caught.getMessage());
			}
		  });
		  
		  cboUnit = new ComboBox();
		  cboUnit.setName("unit");
		  cboUnit.setFieldLabel("Unit"); 
		  cboUnit.setEmptyText("Select Unit...");  
		  cboUnit.setDisplayField("unit");  
		  cboUnit.setStore(storeUnit);  
		  cboUnit.setTypeAhead(true);  
		  cboUnit.setTriggerAction(TriggerAction.ALL); 
		  fieldSet2.add(cboUnit);
	    
		  txtTgl = new DateField();  
		  txtTgl.setName("tanggal");
		  txtTgl.setPropertyEditor(new DateTimePropertyEditor("dd/MM/yyyy"));
		  txtTgl.setFieldLabel("Tanggal");  
		  txtTgl.setData("text", "Enter Tanggal");     
		  txtTgl.setValue(new Date());
		  fieldSet2.add(txtTgl);
	    
		  txtPemakaian = new TextField();
		  txtPemakaian.setName("pakai");
		  txtPemakaian.setFieldLabel("Pakai");  
		  fieldSet2.add(txtPemakaian);
	    
		  txtKeterangan = new TextArea();
		  txtKeterangan.setName("keterangan");
		  txtKeterangan.setFieldLabel("Keterangan");  
		  fieldSet2.add(txtKeterangan);
	    
		  panel.add(fieldSet2);
		  
		  p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
			  @Override  
			  public void componentSelected(ButtonEvent ce) {  
				  Map mapParam = new FastMap();
				  mapParam.put("id_pltu", Registry.get("idPltu"));
				  mapParam.put("id_unit", cboUnit.getValue().get("id_unit"));
				  mapParam.put("volume_pengajuan", txtPemakaian.getRawValue());
				  mapParam.put("keterangan", txtKeterangan.getRawValue());
				  mapParam.put("satuan", "TON");
				  mapParam.put("tanggal", txtTgl.getRawValue());
				  mapParam.put("user", "DHAMARSU");
				  mapParam.put("app_access", "WEB");
				  mapParam.put("no_permintaan",txtNoPermintaan.getRawValue());
				  
				  if (txtNoPermintaan.getRawValue().equals("")){
					  mapParam.put("crud", "CREATE");
				  }else{
					  mapParam.put("crud", "UPDATE");
				  }				  
				  service.insertGudangBB("PEMAKAIAN", mapParam, new AsyncCallback<List<Map>>() {
					
					@Override
					public void onSuccess(List<Map> result) {
						// TODO Auto-generated method stub
						storeResult.removeAll();
	    				storeResult.add(ClientModule.setListBaseModelFromListMap(result));
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						MessageBox.alert("Failed", caught.getMessage(), null);  
					}
				});
			  }  
		  }));  
		  
		  p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
			  @Override  
			  public void componentSelected(ButtonEvent ce) {  
				  panel.reset();
			  }  
		  }));  

		  panel.add(p);
	    
		  return panel;  
	  }  
	  
	  private Grid<PageBbPemasokBean> createGrid() {  
	    
	   
	    final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
	    
	    RowNumberer id = new RowNumberer();  
	    id.setWidth(30);
	    
	    ColumnConfig tgl = new ColumnConfig("head_office", "Unit", 120);
	    tgl.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig volume = new ColumnConfig("jenis_pasokan", "Tanggal", 100);
	    volume.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig satuan = new ColumnConfig("kategori_pemasok", "Pakai", 100);
	    satuan.setAlignment(HorizontalAlignment.LEFT);
	    
	    ColumnConfig ket = new ColumnConfig("nama_pemasok", "Keterangan", 150);
	    ket.setAlignment(HorizontalAlignment.LEFT);
	    
	    columns.add(id);
	    columns.add(tgl);
	    columns.add(volume);
	    columns.add(satuan);
	    columns.add(ket);
	   
	    ListStore<PageBbPemasokBean> store = new ListStore<PageBbPemasokBean>();  
	    store.setMonitorChanges(true);  
//	    store.add(TestData.getPemasok());  
	  
	    final ColumnModel cm = new ColumnModel(columns);
	    
	    grid = new Grid<PageBbPemasokBean>(store, cm);
	    grid.setBorders(true);  
	    
	    return grid;  
	  }  
}
